package com.example.bezierviewpager.slice;


import com.example.bezierviewpager.ResourceTable;

import com.example.library.BezierRoundView;
import com.example.library.GlideImageClient;
import com.example.library.util.ImageLoadFactory;
import com.example.library.vPage.BezierViewPager;
import com.example.library.vPage.CardPagerAdapter;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class MainAbilitySlice extends AbilitySlice {
  @Override
  public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main);

    ImageLoadFactory.getInstance().setImageClient(new GlideImageClient());
    initImgList();

// 获取屏幕密度
    Optional<Display>
        display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
    DisplayAttributes displayAttributes = display.get().getAttributes();
    int mWidth = displayAttributes.width;

    float heightRatio = 0.565f;  //高是宽的 0.565 ,根据图片比例

    CardPagerAdapter cardAdapter = new CardPagerAdapter(getApplicationContext());
    cardAdapter.addImgUrlList(imgList);


    //设置阴影大小，即vPage  左右两个图片相距边框  maxFactor + 0.3*CornerRadius   *2
    //设置阴影大小，即vPage 上下图片相距边框  maxFactor*1.5f + 0.3*CornerRadius
    int maxFactor = mWidth / 25;
    cardAdapter.setMaxElevationFactor(maxFactor);

    int mWidthPading = mWidth / 8;

    //因为我们adapter里的cardView CornerRadius已经写死为10dp，所以0.3*CornerRadius=3
    //设置Elevation之后，控件宽度要减去 (maxFactor + dp2px(3)) * heightRatio
    //heightMore 设置Elevation之后，控件高度 比  控件宽度* heightRatio  多出的部分
    float heightMore = (1.5f * maxFactor + dp2px(3)) - (maxFactor + dp2px(3)) * heightRatio;
    int mHeightPading = (int) (mWidthPading * heightRatio - heightMore);

    BezierViewPager viewPager = (BezierViewPager) findComponentById(ResourceTable.Id_view_page);
    //overScrollMode
    viewPager.setPageMargin(100);
    viewPager.setCentralScrollMode(false);
    viewPager.setLayoutConfig(new DependentLayout.LayoutConfig(mWidth, (int) (mWidth * heightRatio)));
    viewPager.setPadding(mWidthPading, mHeightPading, mWidthPading, mHeightPading);
    viewPager.setClipEnabled(false);
    viewPager.setProvider(cardAdapter);
   // viewPager.showTransformer(0.2f);


    BezierRoundView bezRound = (BezierRoundView) findComponentById(ResourceTable.Id_bezRound);
    bezRound.attach2ViewPage(viewPager);


    Image iv_bg = (Image) findComponentById(ResourceTable.Id_iv_bg);
    iv_bg.setLayoutConfig(new DependentLayout.LayoutConfig(mWidth, (int) ((mWidth * heightRatio) + dp2px(60))));
  }

  /**
   * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
   */
  public int dp2px(float dpValue) {

    // 获取屏幕密度
    Optional<Display>
        display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
    DisplayAttributes displayAttributes = display.get().getAttributes();
    final float scale = displayAttributes.densityPixels;
    //final float scale = getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
  }

  private List<Object> imgList;

  public void initImgList() {
    imgList = new ArrayList<>();
    imgList.add("https://seopic.699pic.com/photo/40168/1186.jpg_wh1200.jpg");
    imgList.add("https://seopic.699pic.com/photo/50178/4265.jpg_wh1200.jpg");
    imgList.add("https://seopic.699pic.com/photo/50178/3504.jpg_wh1200.jpg");
    imgList.add("https://seopic.699pic.com/photo/50178/4263.jpg_wh1200.jpg");

  }


  @Override
  public void onActive() {
    super.onActive();
  }

  @Override
  public void onForeground(Intent intent) {
    super.onForeground(intent);
  }
}
